import 'package:flutter/material.dart';
import 'package:snippet_coder_utils/FormHelper.dart';
import 'package:snippet_coder_utils/list_helper.dart';
import 'package:sqlite_demo/pages/add_edit_product.dart';
import 'package:sqlite_demo/services/db_service.dart';

import '../models/data_model.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  DBService dbService = DBService();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.redAccent,
          title: const Text("FLutter SQFITE CRUD"),
        ),
        body: Column(
          children: [
            const SizedBox(
              height: 10,
            ),
            Align(
              alignment: Alignment.centerRight,
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: FormHelper.submitButton(
                  "add Product",
                  () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const AddEditProduct()),
                    );
                  },
                  borderRadius: 19,
                  btnColor: Colors.lightBlue,
                  borderColor: Colors.lightBlue,
                ),
              ),
            ),
            _fetchData(),
          ],
        ));
  }

  _fetchData() {
    return FutureBuilder<List<ProductModel>>(
        future: dbService.getProducts(),
        builder:
            (BuildContext context, AsyncSnapshot<List<ProductModel>> products) {
          if (products.hasData) {
            return _buildDataTable(products.data!);
          }
          return const Center(
            child: CircularProgressIndicator(),
          );
        });
  }

  _buildDataTable(List<ProductModel> model) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: ListUtils.buildDataTable(context, ['product name', 'price', ''],
          ['productName', 'price', ''], false, 0, model, (ProductModel data) {
        Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => AddEditProduct(
                    isEditMode: true,
                    model: data,
                  )),
        );
      }, (ProductModel data) {
        return showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: const Text("SQFLITE CRUD"),
                content: const Text("Do you want to delete this record ?"),
                actions: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      FormHelper.submitButton("YES", () {
                        dbService.deleteProduct(data).then((value) {
                          setState(() {
                            Navigator.of(context).pop();
                          });
                        });
                      }, width: 100, borderRadius: 5, btnColor: Colors.green),
                      const SizedBox(
                        width: 5,
                      ),
                      FormHelper.submitButton("No", () {
                        setState(() {
                          Navigator.of(context).pop();
                        });
                      }, width: 100, borderRadius: 5, btnColor: Colors.grey),
                    ],
                  )
                ],
              );
            });
      },
          headingRowColor: Colors.orangeAccent,
          isScrollable: true,
          columnTextFontSize: 15,
          columnTextBold: false,
          columnSpacing: 50,
          onSort: (columnIndex, columnName, acc) {}),
    );
  }
}
